package com.howie.parnote.util.jms;

import com.howie.parnote.util.LogUtil;
import org.apache.log4j.Logger;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * <p>Title: parnote</p>
 * <p>
 * Description: parnote
 * </p>
 *
 * @author huangyan
 *         Created by HY on 2015/4/17 0017.
 * @version v1.0.0
 */
public class EmailUtil {
	private static final Logger logger = LogUtil.getLogger(EmailUtil.class);
	private static final String SMTP_PORT = "465";
	private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

	public static final String FROM_EMAIL = "littlehuang1881@sina.com";
	public static final String USER_NAME = "littlehuang1881@sina.com";
	public static final String PASSWORD = "DO522dongwx1881";
	public static final String CONFIRM_SUBJECT = "请确认你的Parnote账号";


	public static void sendTextEmail(String fromEmail, String toEmail,
									 String userName, String password, String subject, String message) {
		JavaMailSenderImpl senderImpl = getJavaMailSender(fromEmail, userName, password);
		SimpleMailMessage mailMessage = new SimpleMailMessage();
		mailMessage.setTo(toEmail);
		mailMessage.setFrom(fromEmail);
		mailMessage.setSubject(subject);
		mailMessage.setText(message);
		senderImpl.send(mailMessage);
	}

	public static void sendHtmlMail(String fromEmail, String toEmail,
									String userName, String password, String subject, String message) {
		JavaMailSenderImpl senderImpl = getJavaMailSender(fromEmail, userName, password);

		MimeMessage mailMessage = senderImpl.createMimeMessage();
		MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
		try {
			messageHelper.setTo(toEmail);
			messageHelper.setFrom(fromEmail);
			messageHelper.setSubject(subject);
			messageHelper.setText(message, true);

			senderImpl.send(mailMessage);
			if (logger.isDebugEnabled()) {
				logger.debug("Html Email send succeed");
			}
		} catch (Exception e) {
			logger.error("Html Email send fail", e);
			e.printStackTrace();
		}
	}

	private static JavaMailSenderImpl getJavaMailSender(String fromEmail, String userName, String password) {
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
		String host = getHost(fromEmail);
		senderImpl.setHost(host);
		senderImpl.setUsername(userName);
		senderImpl.setPassword(password);
		Properties prop = new Properties();
		if (host.equalsIgnoreCase("smtp.gmail.com")) {
			prop.setProperty("mail.smtp.port", SMTP_PORT);
			prop.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
			prop.setProperty("mail.smtp.socketFactory.port", SMTP_PORT);
			prop.setProperty("mail.smtp.socketFactory.fallback", "false");
		}
		prop.setProperty("mail.smtp.auth", "true");
		senderImpl.setJavaMailProperties(prop);
		return senderImpl;
	}

	private static String getHost(String fromEmail) {
		int at = fromEmail.indexOf("@");
		String tmpStr = fromEmail.substring(at + 1);
		String host = "smtp." + tmpStr;
		return host;
	}
}
